/** @file GOTHethernet.h This file contains the source code for the ETHERNET API of the GOTH
 * Kick-ass Etherboard.
 * 
 * The ETHERNET controller must be connected to SPI1
 * The ETHERNET controller is type ENC28J60
 *
 * Sets the SPI1 to a frequency of 16MHz
 *
 *
 * @author Geoffrey Ottoy - Tom Hamelinckx
 * @date 06/07/2011 (last modified)
 *
 * <pre>
 * MODIFICATION HISTORY:
 *
 * Who  Date     Changes
 * ---- -------- ---------------------------------------------------------
 * TH   06/07/11 Created this file.
 * GO   07/07/11 Added eth interrupt (INT3)
 * </pre>
 * 
 */
 
#ifndef __GOTH_ETH_H__
#define __GOTH_ETH_H__

#include "p24fj256GB110.h"
#include "GOTHglobal.h"
#include "GOTHethernetcommands.h"


#define ETH_DisableEthSpi() {SPI1STATbits.SPIEN = 0;}
#define ETH_EnableEthSpi()  {SPI1STATbits.SPIEN = 1;}

#define ETH_HardReset() {LATBbits.LATB15 = 0; LATBbits.LATB15 = 1} //might be too fast!


#define ETH_Select() {LATFbits.LATF8 = 0;}
#define ETH_Deselect() {LATFbits.LATF8 = 1;}

/** Initialize the ETH_INT pin (RP10 resp. RF4 on the kick-ass etherboard)
 *
 * @return	nothing
 */
void ETH_InitInterrupt();

/** Check if the ETH_INT has occured.
 *
 * @return	The number of interrupts (which is than set to 0). 
 */
int ETH_Interrupt();

/** Initialize SPI1 for operation.
 * 
 * @return      nothing
 *
 * @warning Attached memory has a maximum allowed clock of 20 MHz! Do not exceed!
 */
void ETH_InitSpi1(void); 

/** Send data to the ethernet controller and read back its answer.
 * 
 * To keep clocking the device and just waiting for its response, keep sending 0
 *
 * @param       data    One byte of data to send out
 *
 * @return      The response from the readback
 */
unsigned char ETH_TransmitReceive(unsigned char data);

/** Perform a soft reset on the Ethernet controller
 * 
 * Datasheet claims this is EQUAL to hard reset
 *
 * @return	nothing
 */
void ETH_SoftReset(void);

/** Select the bank where the given registername is in
 * 
 *
 * @param		registername	The register of which you want to select the bank
 *
 * @return      nothing
 */
void ETH_SetBank(unsigned char registername);

///Work in progress: Trying something crazy! ;-)

/** Read a control register from the Ethernet controller
 * 
 * This command automaticly selects the correct bank!
 *
 * @param       registername    The register you want to read
 *
 * @return      The register value
 */
unsigned char ETH_ReadControl(unsigned char registername);

/** Write a control register 
 * 
 * This command automaticly selects the correct bank!
 *
 * @param       registername    The register to write to
 *				value			The value to be written
 *
 * @return      nothing
 */
void ETH_WriteControl(unsigned char registername, unsigned char value);

/** Write a PHY Register of the Ethernet controller
 * 
 * This command follows the entire protocol to write to the special PHY registers
 *
 * @param       registername	the PHY-registername
 *				value			Integer (16 bit!) to send
 *
 * @return      nothing
 */
void ETH_WritePHYReg(unsigned char registername, unsigned int value);

/** Read a PHY Register of the Ethernet Controller
 * 
 * This command follows the entire protocol to read from the special PHY registers
 * The function is blocking until the value is ready! (At least 10.24us!)
 *
 * @warning		EXPERIMENTAL
 *
 * @param       registername    the PHY-registername
 *
 * @return      the integer (16bit) that you requested!
 */
unsigned int ETH_ReadPHYReg(unsigned char registername);
 
 
 
#endif
